package category

import (
	"context"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/golang/protobuf/ptypes/empty"
	"go.uber.org/zap"
	"hosptial-record/api/base"
	"hosptial-record/forms"
	"hosptial-record/global"
	"hosptial-record/global/response"
	"hosptial-record/proto"
	"net/http"
)

// Create 创建分类
func Create(ctx *gin.Context) {
	var form forms.CategoryForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}
	// 计算level, 默认一级
	level := int32(1)
	if form.ParentCategoryID != 0 {
		// 获取父级信息
		res, err := global.GoodsSrvClient.GetSubCategory(ctx, &proto.CategoryListRequest{Id: form.ParentCategoryID})
		if err != nil {
			base.HandleGrpcErrorToHttp(err, ctx)
			return
		}
		level = res.Info.Level + 1
	}
	// 创建分类
	params := proto.CategoryInfoRequest{
		Name:           form.Name,
		ParentCategory: form.ParentCategoryID,
		Level:          level,
		IsTab:          form.IsTab,
		Url:            form.Url,
	}
	if _, err := global.GoodsSrvClient.CreateCategory(ctx, &params); err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "创建成功",
	})
}

// Update 更新分类信息
func Update(ctx *gin.Context) {
	var form forms.UpdateCategoryForm
	if err := ctx.ShouldBind(&form); err != nil {
		base.HandleValidatorError(ctx, err)
		return
	}

	params := proto.CategoryInfoRequest{
		Id:    form.ID,
		Name:  form.Name,
		IsTab: form.IsTab,
		Url:   form.Url,
	}
	// 计算level, 默认一级
	level := int32(1)
	if form.ParentCategoryID != 0 {
		// 获取父级信息
		res, err := global.GoodsSrvClient.GetSubCategory(ctx, &proto.CategoryListRequest{Id: form.ParentCategoryID})
		if err != nil {
			base.HandleGrpcErrorToHttp(err, ctx)
			return
		}
		level = res.Info.Level + 1
		params.ParentCategory = form.ParentCategoryID
	}
	params.Level = level
	if _, err := global.GoodsSrvClient.UpdateCategory(context.Background(), &params); err != nil {
		zap.S().Errorf("修改分类失败:%s", err.Error())
		return
	} else {
		ctx.JSON(http.StatusOK, response.BaseResponse{
			Msg: "修改成功",
		})
	}
}

// Delete 删除分类
func Delete(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		return
	}
	if _, err := global.GoodsSrvClient.DeleteCategory(context.Background(), &proto.DeleteCategoryRequest{Id: id}); err != nil {
		zap.S().Errorf("删除分类失败:%s", err.Error())
		base.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg: "删除分类成功",
	})
}

// List 获取所有分类
func List(ctx *gin.Context) {
	res, err := global.GoodsSrvClient.GetAllCategoriesList(ctx, &empty.Empty{})
	if err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
	}
	// 将json转义
	var jData []response.CategoryResponse
	err = json.Unmarshal([]byte(res.JsonData), &jData)
	if err != nil {
		zap.S().Errorf("转义json失败：%s", err.Error())
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg:  "获取成功",
		Data: jData,
	})
}

// GetCategoryByID 通过父ID获取分类列表
func GetCategoryByID(ctx *gin.Context) {
	id, err := base.GetID2Param(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, response.BaseResponse{
			Msg: "传入的ID不合法",
		})
		return
	}
	params := proto.CategoryListRequest{
		Id: id,
	}
	var res *proto.SubCategoryListResponse
	res, err = global.GoodsSrvClient.GetSubCategory(ctx, &params)
	if err != nil {
		base.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, response.BaseResponse{
		Msg:  "获取成功",
		Data: res.SubCategorys,
	})
}
